/**
 * Created by wdj on 16/4/21.
 * HybridController
 */
'use strict';
var Controller = require('../Controller');
var Utils = require('../../libs/utils');
var hybrid = require('../../libs/hybrid');

class HybridController extends Controller {

    onCreated(app, page) {


        this.bindEvents({
            'click a.load-about': () => this.loadView('about'),

            'click a.getDeviceInfo': () => hybrid.getDeviceInfo((sts, data) => {
                app.alert(JSON.stringify(data))
            }),

            'click a.getNetworkStatus': () => {
                hybrid.getNetworkStatus((sts, info) => {
                    app.alert(JSON.stringify(info), sts);
                })
            },

            'click a.setTheme': () => {
                hybrid.setTheme('#ffffff', '#D32C43', true, (sts, info) => {
                    app.alert(JSON.stringify(info));
                });
            },

            'click a.openWindow': () => {
                hybrid.openWindow({
                    url: 'http://m.baidu.com',
                    showMenu: true,
                });
            },

            'click a.openURL': () => {
                hybrid.openURL('https://m.baidu.com');
            },

            'click a.playVibrate': () => {
                hybrid.playVibrate();
            },

            'click a.playSound': () => {
                //var filename = app.device.ios ? 'ele.caf' : 'ele.mp3';
                hybrid.playSound('xxms.caf', function(sts, res) {
                    app.alert(JSON.stringify(res));
                });

                setTimeout(function() {
                    hybrid.playSound('xxms2.caf');
                }, 5000);

                setTimeout(function() {
                    hybrid.playSound('tsy.caf');
                }, 8000);
            },

            'click a.setIconBadge': () => {
                hybrid.setIconBadge(Math.floor(Math.random() * 10));
            },

            'click a.getGeTuiClientId': () => {
                hybrid.getGeTuiClientId((sts, info) => {
                    app.alert(JSON.stringify(info));
                })
            },

            'click a.showToast': () => {
                hybrid.showToast('........');
            },

            'click a.showIndicator': () => {
                hybrid.showIndicator();
                setTimeout(hybrid.hideIndicator, 2000);
            },

            'click a.showAlert': () => {
                hybrid.showAlert('Hello ');
            },

            'click a.showConfirm': () => {
                hybrid.showConfirm({
                    message: '确认对话框?',
                }, function(sts) {
                    app.alert(sts);
                });
            },

            'click a.getCurrentLocation': () => {
                hybrid.geolocation.getCurrentLocation((sts, info) => {
                    app.alert(JSON.stringify(info), sts);
                });
            },

            'click a.scanQRCode': () => {
                var QRCode = hybrid.plugin.QRCode;
                QRCode.scan({
                    title: "扫码",
                    label: "请将二维码对准框内容"
                }, function(result) {
                    app.alert(JSON.stringify(result));
                });
            },

            'click a.aliPay': () => {
                var AliPay = hybrid.plugin.AliPay;
                AliPay.payOrder({
                    order: {
                        "partner": "2088911200828478",
                        "sellerId": "2088911200828478",
                        "outTradeNo": "00141583636022863819412871",
                        "subject": "账户充值",
                        "body": "账户充值",
                        "totalFee": "1.00",
                        "notifyUrl": "http://statics.maosui.com/Res/AlipayNotify.ashx",
                        "service": "mobile.securitypay.pay",
                        "paymentType": "1",
                        "inputCharset": "utf-8",
                        "itBPay": "10m",
                        "showUrl": "http://statics.maosui.com/Res/AlipayNotify.ashx",
                        "sign": "jDv1kxozcshLBuox9NmuP66Dn/OYzk33aeSDqL3VEaylo6SqoTc+bDADcACOW96uLs/BWyDl/1pd+uNO8z42s/FqUbBDFfnlvrmd7L5Wnt8rfwKVuEbAHIVuX77pyvlXR7asXjLFvGGywZWTGDiCW9pVP0uZ86be8KNWYQ6qBAY=",
                        "signType": "RSA"
                    }
                }, function(result) {
                    app.alert(JSON.stringify(result));
                });
            },
            'click a.openGPSSettings': () => {
                if (navigator.userAgent.match(/Android/i)) {
                    //android
                    hybrid.openURL({
                        action: 'android.settings.LOCATION_SOURCE_SETTINGS',
                    });
                }

                if (navigator.userAgent.match(/iPhone/i)) {
                    //ios
                    hybrid.openURL('prefs:root=Apps&path=毛遂自荐');
                }
            },
            'click a.test-showNotify': () => {
                hybrid.showNotify({
                    title: '设置badge',
                    message: '测试内容,设置badge 为 1',
                    iconBadge: 10
                }, (sts, result) => {
                    app.alert(JSON.stringify(result));
                });
            },
            'click a.show-notify-audio': () => {
                hybrid.showNotify({
                    title: '设置badge',
                    message: '测试内容,设置badge 为 1',
                    sound: 'xxms.caf',
                    iconBadge: 10,
                    data: {
                        name: 'jack'
                    }
                }, (sts, result) => {
                    app.alert(JSON.stringify(result));
                });
            },
            'click a.save-data-to-store': () => {
                hybrid.store.setValue('test-store', {
                    name: 'jack',
                    userId: 'Abc'
                }, (sts, result) => {
                    app.alert(JSON.stringify(result));
                })
            },
            'click a.get-data-from-store': () => {
                hybrid.store.getValue('test-store', (sts, value) => {
                    app.alert(JSON.stringify(value), typeof value);
                });
            }
        });
    }
}

export default HybridController;